﻿Ext.define("PSI.BaseCode.Admin.Metadata.MetadataEditForm", {

    createUI: function (parementid) {

        var me = this;

        var AddMetadatawindow = Ext.create("Ext.window.Window", {
            title: "新增码表主属性",
            modal: true,
            onEsc: Ext.emptyFn,
            width: 400,
            height: 180,
            border: false,
            layout: "fit",
            items: [
                 {
                     id: "MetadataForm",
                     xtype: "form",
                     layout: "vbox",
                     height: "100%",
                     width: 300,
                     bodyPadding: 5,
                     defaultType: 'textfield',
                     defaults: { // defaults are applied to items, not the container
                         margin: "10 0 0 0",
                         width: 370
                     },
                     bodyStyle: {
                         // background: '#ffc',
                         padding: '10px'
                     },
                     fieldDefaults: {
                         labelWidth: 120,
                         labelAlign: "right",
                         labelSeparator: "",
                         msgTarget: 'side'
                     },
                     items: [
                           {
                               xtype: "hidden",
                               name: "basecodeId",
                               value: null
                           },
                           {
                               id: "cnname",
                               fieldLabel: "码表中文名",
                               allowBlank: false,
                               blankText: "没有输入名称",
                               beforeLabelTextTpl: PSI.Const.REQUIRED,
                               name: "basecodeName",
                               value: null
                           },
                           {
                               id: "tablename",
                               fieldLabel: "码表对应数据库名",
                               allowBlank: false,
                               blankText: "没有输入名称",
                               beforeLabelTextTpl: PSI.Const.REQUIRED,
                               name: "tableName",
                               value: null

                           },
                           {
                               id: "yongtu",
                               fieldLabel: "码表用途",
                               name: "baseCodeMemo",
                               value: null
                           }
                     ],
                     buttons: [
                        {
                            text: "确定",
                            formBind: true,
                            iconCls: "psi-button-ok",
                            handler: function () { this.createTable(parementid) },
                            scope: this
                        },
                        {
                            text: "取消", handler: function () {
                                PSI.MsgBox.confirm("请确认是否取消操作?", function () {
                                    me.addbasecodewindow.close();
                                });
                            }, scope: this
                        }
                     ]
                 }

            ]

        }).show();

        me.addtablewindow = AddMetadatawindow;

    },

    show: function (parementid) {

        return this.createUI(parementid);

    },

    createTable: function (id) {
     
        var win = this.addtablewindow

        var category = Ext.getCmp("caregorytree"); 
        var items = category.getSelectionModel().getSelection();
        var parentid = items[0].data.id;

        var f = Ext.getCmp("MetadataForm");
        var el = f.getEl();
        el.mask("数据保存中...");
        f.submit({
            url: "/BaseCodeAdminMetadata/AddMetadata",
            method: "POST",
            params: {
                categoryid: id
            },
            success: function (form, action) {
                el.unmask();
                var cc = action.result.success;
                if (cc) {
                    PSI.MsgBox.showInfo("数据保存成功", function () {
                        win.close();

                        var stroe = category.getStore();

                        var id = (action.result.id).toUpperCase();

                        stroe.load({
                            scope: this,
                            callback: function (records, operation, success) {
                                
                                var Newstror = category.getStore();

                                var note = Newstror.getNodeById(id)

                                category.getSelectionModel().select(note);
                            }
                        });
                        
                    });
                }
                else { 
                    Ext.Msg.alert('Failure', action.result.msg);
                }
            },
            failure: function (form, action) {
                el.unmask();
                switch (action.failureType) {
                    case Ext.form.action.Action.CLIENT_INVALID:
                        Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                        break;
                    case Ext.form.action.Action.CONNECT_FAILURE:
                        Ext.Msg.alert('Failure', 'ajax链接失败!');
                        break;
                    case Ext.form.action.Action.SERVER_INVALID:
                        Ext.Msg.alert('Failure', action.result.msg);
                }
            }
        });
    }


})